package com.zhika.zishi.fragments.login;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;

import com.zhika.zishi.R;
import com.zhika.zishi.adapter.SelectHobbyAdapter;
import com.zhika.zishi.fragments.BackHandlerFragment;

import java.util.ArrayList;

/**
 * Created by Michael on 2015/4/6.
 * 选择喜欢的攻略、用户的性别
 */
public class SelectHobbyFragment extends BackHandlerFragment implements View.OnClickListener {

    private GridView gridView;

    private SelectHobbyAdapter adapter;

    private ArrayList<String> arrayList = new ArrayList<>();

    private EditText customHobbyEdit;

    private Button commitButton;

    //该界面的view
    private View view;
    
    private Context context;

    private Button sureButton;

    @Override
    public boolean onBackPressed() {
        return true;
    }

    /**
     * 在activity中实现此接口的方法，控制该fragment的事件
     */
    public interface OnSelectHobbyListener{

        //确定按钮的事件
        public void selectHobbySure(String likeStrategy);

    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.select_hobby_layout,container,false);
        
        addStrTest();
        
        initView();
        
        return view;
    }

    private void initView() {
        context = getActivity();
        
        gridView = (GridView) view.findViewById(R.id.select_hobby_grid_view);
        adapter = new SelectHobbyAdapter(context, arrayList);
        gridView.setAdapter(adapter);

        customHobbyEdit = (EditText) view.findViewById(R.id.select_hobby_custom_edit);
        commitButton = (Button) view.findViewById(R.id.select_hobby_commit_button);
        commitButton.setOnClickListener(this);

        sureButton = (Button) view.findViewById(R.id.select_hobby_sure_button);
        sureButton.setOnClickListener(this);
    }

    /**
     * 测试数据
     */
    private void addStrTest() {
        for (int i = 0; i < 9; i++) {
            arrayList.add("爱好" + i);
        }
    }

    @Override
    public void onClick(View v) {
        if (context instanceof OnSelectHobbyListener){
            switch (v.getId()){
                case R.id.select_hobby_commit_button:
                    arrayList.add(customHobbyEdit.getText().toString());
                    adapter.notifyDataSetChanged();
                    break;
                case R.id.select_hobby_sure_button:
                    ((OnSelectHobbyListener)context).selectHobbySure("dd");
                    break;
            }
        }
    }
}
